package com.hpin.fansea.my;

import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hpin.fansea.R;
import com.hpin.fansea.adapter.MyOrderListAdapter;
import com.hpin.fansea.base.BaseActivity;
import com.hpin.fansea.bean.MyOrderResult;
import com.hpin.fansea.pulltorefush.PullToRefreshBase;
import com.hpin.fansea.pulltorefush.PullToRefreshListView;
import com.hpin.fansea.ui.HomeActivity;
import com.hpin.fansea.utils.Constant;
import com.hpin.fansea.utils.HttpUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的 订单详情页面
 * Created by Aaron on 15/9/21.
 */
public class MyOrderActivity extends BaseActivity {

    private PullToRefreshListView my_order_list;
    private List<MyOrderResult.OrderDetail> detailList = new ArrayList<MyOrderResult.OrderDetail>();
    private MyOrderListAdapter mAdapter;
    private int pageIndex = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_order);

        initWidget();

    }

    @Override
    protected void onResume() {
        super.onResume();

        pageIndex = 0;

        if (detailList!=null)
            detailList.clear();

        getMyOrder();
    }

    private void initWidget() {

        my_order_list = (PullToRefreshListView) findViewById(R.id.my_order_list);

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);

        tv_title.setText(getResources().getString(R.string.my_order));

        iv_left.setOnClickListener(this);

        my_order_list.setMode(PullToRefreshBase.Mode.BOTH);

        mAdapter = new MyOrderListAdapter(MyOrderActivity.this, detailList);
        my_order_list.setAdapter(mAdapter);

        my_order_list.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {


                //pulltorefushview 上次刷新时间
                String label = DateUtils.formatDateTime(MyOrderActivity.this, System.currentTimeMillis(),
                        DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);

                // Update the LastUpdatedLabel
                refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);

                pageIndex = 0;
                detailList.clear();
                getMyOrder();

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

                //pulltorefushview 上次刷新时间
                String label = DateUtils.formatDateTime(MyOrderActivity.this, System.currentTimeMillis(),
                        DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);

                // Update the LastUpdatedLabel
                refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);

                getMyOrder();
            }
        });

    }

    /**
     * 获得订单列表
     */
    private void getMyOrder() {

        HttpUtil.getInstance().setGetRequest("获取中...", this, Constant.WEB_ROOT + Constant.MYORDERLIST
                + sp.getString(Constant.userId, "") + "/" + "orderList" + "?token="
                + Constant.token + "&systemType=" + Constant.SYSTEM_TYPE
                + "&orderBy=" + "" + "&orderSeq=" + "" + "&categoryId=" + ""
                + "&pageSize=" + Constant.PAGECOUNT + "&pageIndex=" + (++pageIndex), new HttpUtil.SuccessListener() {
            @Override
            public void setOnSuccessListener(String str) {

                try {

                    my_order_list.onRefreshComplete();

                    MyOrderResult orderResult = JSONObject.parseObject(str, MyOrderResult.class);

                    if (Constant.STATUS_SUCESS.equals(orderResult.status)) {

                        setListView(orderResult.result);

                        if (detailList != null && mAdapter != null) {
                            mAdapter.setData(detailList);
                            mAdapter.notifyDataSetChanged();
                        }

                    } else {
                        showToast(orderResult.message);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    showToast("获取订单失败");
                }

            }
        }, new HttpUtil.FailListener() {
            @Override
            public void setOnFailListener() {
                my_order_list.onRefreshComplete();
            }
        });

    }

    /**
     * 判断是否有重复并更新
     */
    private void setListView(List<MyOrderResult.OrderDetail> tempGuideLists) {

        if (tempGuideLists == null || tempGuideLists.size() == 0)
            return;

        if (detailList.size() == 0) {

            detailList.addAll(tempGuideLists);
            return;
        }

        for (MyOrderResult.OrderDetail goods : tempGuideLists) {
            boolean exist = false;
            for (MyOrderResult.OrderDetail g : detailList) {
                if (goods.id_.equals(g.id_)) {
                    exist = true;
                    break;
                }
            }
            if (!exist)
                detailList.add(goods);
        }

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();

        Intent mIntent = new Intent(this, HomeActivity.class);
        startActivity(mIntent);
        finish();

    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.iv_left:

                Intent mIntent = new Intent(this, HomeActivity.class);
                startActivity(mIntent);

                finish();
                break;

        }
    }

}
